package com.oracle.zebrafreelytraveling.ui.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.oracle.zebrafreelytraveling.R;
import com.oracle.zebrafreelytraveling.data.RequestResult.ResCard;

import com.oracle.zebrafreelytraveling.ui.BaseRecycleAdapter.CommonAdapter;
import com.oracle.zebrafreelytraveling.ui.BaseRecycleAdapter.MultiItemTypeAdapter;
import com.oracle.zebrafreelytraveling.ui.BaseRecycleAdapter.base.ItemViewDelegate;
import com.oracle.zebrafreelytraveling.ui.BaseRecycleAdapter.base.ViewHolder;
import com.oracle.zebrafreelytraveling.utils.constant.Constant;
import com.oracle.zebrafreelytraveling.utils.ConvertUtils;
import com.oracle.zebrafreelytraveling.utils.StringUtil;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yydcnjjw on 2017/7/26.
 */

public class CardsListAdapter extends CommonAdapter<ResCard.DataBean> {

    public CardsListAdapter(Context context, int layoutId, List<ResCard.DataBean> datas) {
        super(context, layoutId, datas);
    }

    @Override
    protected void convert(ViewHolder holder, ResCard.DataBean dataBean, int position) {
        holder.setText(R.id.user_name, dataBean.getUserName());
        holder.setText(R.id.card_receive, dataBean.getCard().getReceive() + "");
        TagFlowLayout labelLayout = holder.getView(R.id.card_label);
        String[] label = StringUtil.formatLabelList(dataBean.getCard().getLabel());
        if (label != null){
            labelLayout.setAdapter(new TagAdapter<String>(label) {
                @Override
                public View getView(FlowLayout parent, int position, String s) {
                    TextView tv = (TextView) mInflater.inflate(R.layout.tag_card_label_tv,
                            labelLayout, false);
                    tv.setText(s);
                    return tv;
                }
            });
        }
        holder.setText(R.id.card_list_content, dataBean.getCard().getContent());
        String time = dataBean.getCard().getTime();
        holder.setText(R.id.card_time, ConvertUtils.getNormalTimeFormat(time));
        SimpleDraweeView headerImg = holder.getView(R.id.card_list_header);
        headerImg.setImageURI(Constant.DOWNLOAD_URL + dataBean.getHeadImg());
        RecyclerView contentImgRecycle = holder.getView(R.id.card_list_content_img);
        List<String> imgList = StringUtil.formatImgList(dataBean.getCard().getImg());
        if (imgList == null) {
            return;
        }
        if (imgList.size() > 9){
            for (int i = 9; i < imgList.size(); i++) {
                imgList.remove(i);
            }
        }
        if (imgList.size() == 1){
            contentImgRecycle.setLayoutManager(new LinearLayoutManager(mContext));
        } else if (imgList.size() % 2 == 0){
            contentImgRecycle.setLayoutManager(new GridLayoutManager(mContext, 2));
        } else if (imgList.size() % 2 != 0 && imgList.size() != 1){
            contentImgRecycle.setLayoutManager(new GridLayoutManager(mContext, 3));
        }
        contentImgRecycle.setAdapter(new CommonAdapter<String>(mContext, R.layout.item_card_img, imgList) {
            @Override
            protected void convert(ViewHolder holder, String s, int position) {
                SimpleDraweeView image = holder.getView(R.id.card_list_image);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
                params.height = ConvertUtils.dp2px(90);
                params.topMargin = ConvertUtils.dp2px(10);
                params.bottomMargin = ConvertUtils.dp2px(10);
                params.leftMargin = ConvertUtils.dp2px(5);
                params.rightMargin = ConvertUtils.dp2px(5);
                image.setLayoutParams(params);
                image.setImageURI(Constant.DOWNLOAD_URL + s);
            }
        });

    }
}
